package cycling.taxi.com.test;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Messenger;
import android.widget.Toast;

import com.baidu.location.BDLocation;

public class MyService extends Location {
    public final static int SEND_FAIL = 1;
    public Messenger cMessenger;//客服端的信使
//    public int count=0;

    @Override
    public IBinder onBind(Intent intent) {
        return myMessenger.getBinder();
    }

    Handler handler = new Handler() {
        public void handleMessage(Messenger msg) {
            cMessenger = msg;
            startLocation(getApplicationContext());
            Toast.makeText(getApplicationContext(), "服务端接受到了东西", Toast.LENGTH_SHORT).show();

            //也可以给客服端发送消息
//           cMessenger=msg.replyTo;
//           Message message=Message.obtain(null,SEND_FAIL);
//           message.replyTo = myMessenger;
//           try {
//               cMessenger.send(message);
//           }catch (RemoteException e) {
//                e.printStackTrace();
//           }
        }


    };
    private Messenger myMessenger = new Messenger(handler);

    private void startLocation(Context context) {
        int flag = 3;
        if (flag == 3) {

        } else {

        }
        this.localtionGeographyRegion(context);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.distory();
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null) {
            return;
        }
        if (location.getLocType() == 161) {//如果定位成功
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            Toast.makeText(getApplicationContext(), "当前经度:" + longitude + "当前纬度:" + latitude, Toast.LENGTH_SHORT).show();
//            count++;
//            if(count==5){
            //给客服端发送消息
//                Message message=Message.obtain(null,SEND_FAIL);
//                try {
//                    cMessenger.send(message);
//                } catch (RemoteException e) {
//                    e.printStackTrace();
//                }
//            }
        }
    }
}
